package com.gzsxy.esjy.common;

import java.util.Collection;
import java.util.Map;
import java.util.Set;

/**
 * 自定义断言
 * @author zhangxiaolong
 * @date 2022/7/6 9:36
 */
public final class Assert {
	
	private final static int DEFAULT_ERROR=500 ;
	
	/**
	 * 预测结果为true
	 * @param cause
	 * @param msg
	 * @param e
	 */
	public static void notTrue(boolean cause,String msg,int code,Throwable e) {
		if(!cause) {
			throw SvException.getInstance(msg, code, e) ;
		}
	}
	
	/**
	 * 预测结果为true
	 * @param cause
	 * @param msg
	 */
	public static void notTrue(boolean cause,String msg) {
		notTrue(cause, msg, DEFAULT_ERROR, null);
	}
	
	/**
	 * 预测结果为true
	 * @param cause
	 * @param msg
	 * @param code
	 */
	public static void notTrue(boolean cause,String msg,int code) {
		notTrue(cause, msg, code, null);
	}
	
	/**
	 * 预测为空或数组长度为0
	 * @param cause
	 * @param msg
	 * @param code
	 * @param e
	 */
	public static void isBlank(Object cause,String msg,int code,Throwable e) {
		notTrue(cause!=null, msg, code, e);
		if(cause instanceof String) {
			notTrue(!cause.equals(""), msg, code, e);
		}
		if(cause instanceof Collection<?>) {
			notTrue(!((Collection<?>)cause).isEmpty(), msg, code, e);
		}
		if(cause instanceof Map<?, ?>) {
			notTrue(!((Map<?, ?>)cause).isEmpty(), msg, code, e);
		}
		if(cause instanceof Set<?>) {
			notTrue(!((Set<?>)cause).isEmpty(), msg, code, e);
		}
	}
	
	/**
	 * 预测为空或数组长度为0
	 * @param cause
	 * @param msg
	 */
	public static void isBlank(Object cause,String msg) {
		isBlank(cause, msg, DEFAULT_ERROR, null);
	}
	
	/**
	 * 预测为空或数组长度为0
	 * @param cause
	 * @param msg
	 * @param code
	 */
	public static void isBlank(Object cause,String msg,int code) {
		isBlank(cause, msg, code, null);
	}
	
	/**
	 * 预测为空
	 * @param cause
	 * @param msg
	 * @param code
	 * @param e
	 */
	public static void isNotNull(Object cause,String msg,int code,Throwable e) {
		notTrue(cause!=null, msg, code, e);
	}
	
	/**
	 * 预测为空
	 * @param cause
	 * @param msg
	 */
	public static void isNotNull(Object cause,String msg) {
		isNotNull(cause, msg, DEFAULT_ERROR, null);
	}
	
	/**
	 * 预测为空
	 * @param cause
	 * @param msg
	 * @param code
	 */
	public static void isNotNull(Object cause,String msg,int code) {
		isNotNull(cause, msg, code, null);
	}
	
}
